package cn.tedu.v3_1;
/**
 * 多人聊天室version 3.1
 */

import java.io.*;
import java.net.*;
import java.util.*;

public class Server {
    private static final Integer PORT = 1234;
    private ServerSocket serverSocket;
    private String ip;       //String ip=null    申请端口

    public Server() {
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (BindException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            while (true) {
                System.out.println("等待客户端的连接");
               Socket socket=serverSocket.accept();
                new Thread(new ClientHandler(socket)).start();
            }
        } catch (IOException e) {
            System.out.println(ip + "客户端链接失效~~");
        } finally {
            if (serverSocket != null && !serverSocket.isClosed()) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    System.out.println("服务器关闭失败~~");
                }
                System.out.println("服务器关闭成功~~");
            }
        }
    }

    public static void main(String[] args) {
        new Server().start();
    }
//ClientHandler多线程类：控制客户端发来的通讯消息
    private class ClientHandler implements Runnable {

        private Socket socket;
        private String ip;
        //Server ---ClientHandler类  ---输入   输出    流   关闭
        //Client --- start()   ---输入    输出   流  关闭
        private PrintWriter pw;
        private BufferedReader br;

        public ClientHandler(Socket s) {
            //1.明确接入客户端  通讯类   Socket
            this.socket = s;
            //2.给明确客户端IP地址
            ip = socket.getInetAddress().getHostAddress();
            System.out.println("客户端：" + ip + "连接上了");
            //3.给明确接入客户端，读写功能
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
                pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
            } catch (UnsupportedEncodingException e){
                System.out.println();
            }catch (SocketException e){
                System.out.println();
            }
            catch (IOException e) {
                System.out.println("客户端断开链接~");
            }
        }

        @Override
        public void run() {
            try {
                new Thread(() -> {
                    try {
                        String message;
                        while ((message = br.readLine()) != null) {
                            System.out.println(message);
                            System.out.println("服务器请输入：");
                        }
                    } catch (IOException e) {
                        System.out.println("服务器端发来的信息断开了");
                    }
                }).start();
                Scanner input = new Scanner(System.in);
                while (true) {
                    System.out.println("服务器输入");
                    String say = input.nextLine();
                    pw.println("服务器说：" + say);
                }
            } finally {
                if (br != null)
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (pw != null)
                    pw.close();
                if (socket != null && !socket.isClosed()) {
                    System.out.println(ip + "一个客户端退出服务器链接");
                    try {
                        socket.close();
                    } catch (IOException e) {
                        System.out.println(ip + "一个客户端退出链接异常~~");
                    }
                }
            }
        }
    }
}
